package com.wyd.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyd.api.TcProductService;
import com.wyd.core.common.ExampleAdapter;
import com.wyd.entity.TcProduct;
import com.wyd.mapper.TcProductMapper;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Component
@Service
public class TcProductServiceImpl implements TcProductService {

	private static final Logger logger = Logger.getLogger(TcProductServiceImpl.class);

	@Autowired
	private TcProductMapper tcProductMapper;

	@Override
	public int save(TcProduct entity) {
		// TODO Auto-generated method stub
		entity.setIsDelete(0);
		entity.setCreateDate(new Date());
		entity.setModifyDate(new Date());
		return tcProductMapper.insertSelective(entity);
	}

	@Override
	public int delete(Object key) {
		// TODO Auto-generated method stub
		return tcProductMapper.deleteByPrimaryKey(key);
	}

	@Override
	public int updateAll(TcProduct entity) {
		// TODO Auto-generated method stub
		return tcProductMapper.updateByPrimaryKey(entity);
	}

	@Override
	public int updateNotNull(TcProduct entity) {
		// TODO Auto-generated method stub
		entity.setModifyDate(new Date());
		return tcProductMapper.updateByPrimaryKeySelective(entity);
	}

    @Override
    public int updateByExampleSelective(TcProduct entity,Object example){
        entity.setModifyDate(new Date());
        return tcProductMapper.updateByExampleSelective(entity,ExampleAdapter.tran(example));
    }

	@Override
	public TcProduct selectByKey(Object key) {
		// TODO Auto-generated method stub
		return tcProductMapper.selectByPrimaryKey(key);
	}

	@Override
	public List<TcProduct> selectByExample(TcProduct entity) {
		Example example = new Example(TcProduct.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("isDelete", 0);
		example.orderBy("createDate").desc();
		return tcProductMapper.selectByExample(example);
	}

    @Override
    public List<TcProduct> selectByExample(Object example) {
        return tcProductMapper.selectByExample(ExampleAdapter.tran(example));
    }

	@Override
	public List<TcProduct> selectAll() {
		// TODO Auto-generated method stub
		return tcProductMapper.selectAll();
	}

	@Override
	public TcProduct selectByOther(TcProduct entity) {
		// TODO Auto-generated method stub
		return tcProductMapper.selectOne(entity);
	}
	
	@Override
	public PageInfo<TcProduct> selectPageList(Object example,int pageIndex,int pageSize){
		PageHelper.startPage(pageIndex, pageSize, true);
		TcProduct obj = (TcProduct)example;
		List<TcProduct> list = this.selectByExample(obj);
		PageInfo<TcProduct> pageInfo = new PageInfo<TcProduct>(list);
		logger.info("pageInfo Pages:" + pageInfo.getPages()+ "total:" + pageInfo.getTotal());
		return pageInfo;
	}

	@Override
    public int selectCountByExample(Object example){
        return tcProductMapper.selectCountByExample(ExampleAdapter.tran(example));
    }

    @Override
    public PageInfo<TcProduct> selectPageByExample(Object example,int pageIndex,int pageSize){
    	PageHelper.startPage(pageIndex, pageSize, true);
    	List<TcProduct> list = tcProductMapper.selectByExample(ExampleAdapter.tran(example));
    	PageInfo<TcProduct> pageInfo = new PageInfo<TcProduct>(list);
    	return pageInfo;
    }
}